<template>
  <div>
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="文章标题" prop="name">
        <el-input style="width: 550px;" maxlength="50" v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="region">
        <el-select v-model="form.category" placeholder="请选择文章分类">
          <el-option label="前端" value="shanghai"></el-option>
          <el-option label="后端" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章概要" prop="name">
        <el-input type="textarea" v-model="form.summary"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="name">
        <VueUEditor></VueUEditor>
      </el-form-item>
      <el-form-item label="热门" prop="resource">
        <el-radio-group v-model="form.is_hot">
          <el-radio label="是"></el-radio>
          <el-radio label="否 "></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="浏览次数" prop="desc">
        <el-input disabled type="text" v-model="form.view"></el-input>
      </el-form-item>
      <el-form-item label="评论次数" prop="desc">
        <el-input disabled type="text" v-model="form.comment_count"></el-input>
      </el-form-item>
      <el-form-item label="收藏次数" prop="desc">
        <el-input disabled type="text" v-model="form.like_count"></el-input>
      </el-form-item>
      <el-form-item>
        <!-- <el-button type="danger" @click="submitForm('form')">保存</el-button> -->
        <el-button type="danger" @click="submit">保存</el-button>
        <!-- <el-button @click="resetForm('form')">重置</el-button> -->
        <el-button @click="$router.back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import VueUEditor from 'vue-ueditor'
import {getArticle, updateArticle, createArticle} from 'api/article'
export default {
  components: {
    VueUEditor
  },
  data () {
    return {
      form: {
        title: '',
        summary: '',
        content: '',
        is_hot: 0,
        view: 0,
        category_id: 0,
        like_count: 0,
        comment_count: 0
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        category_id: [
          { required: true, message: '请选择文章类型', trigger: 'change' }
        ],
        summary: [
          { required: true, message: '文章概要不能为空', trigger: 'blur'}
        ],
        content: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ],
        is_hot: [
          { required: true, message: '请选择该文章是否热门', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isEdit () {
      if (this.$route.params.article_tid) {
        return true
      } else {
        return false
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      this._getData()
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEdit) {
              this._updateSingleData()
          } else {
              this._createSingleData()
          }
        }
      })
    },
    _updateSingleData () {
      // 还有tid
      delete this.form.view
      delete this.form.like_count
      delete this.form.comment_count
    },
    _createSingleData () {
      createArticle(this.form).then(res => {
        this.$router.back()
      })
    },
    _getData () {
      getArticle({tid: this.$route.params.article_tid}).then(res => {
        const form = res.data
        this.form = {
          title: form.title,
          summary: form.summary,
          content: form.content,
          is_hot: form.is_hot,
          view: form.view,
          like_count: form.like_count,
          comment_count: form.comment_count,
          create_time: form.create_time
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
